/*
 * 整车状态图表
 */
var carload = {
	init : function(){
		this.setMachineDashboard('machineLeft1_1','电池电压','V',0,800 );
		this.setMachineDashboard('machineLeft2_1','电池电流','A',-400,600);
		this.setMachineDashboard('machineRight1_1','电机温度','℃', -40,160 );
		this.setMachineDashboard('machineRight2_1','电机控制器温度','℃',-40,160);
		this.setMachineDashboard('machineRight1_2','电机转速','x1000 r/min',0,10);
		this.setMachineDashboard('machineRight2_2','电机功率','KW',-40,160 );
		this.setMachineDashboard('machineRight1_3','电机电压','V', 0,800);
		this.setMachineDashboard('machineRight2_3','电机电流','A', -400,600);
	},
	//全部图表的resize
	resizeAll : function(){
		var chartIds = [];
		for(var i in this){
			if((typeof this[i]) === 'object' && i.indexOf('Chart') >= 0){
				this[i].resize();
			}
		}
	},
   	//整机状态
	setMachineDashboard : function(id,chartName,unit,lower,upper){
		var chart =  echarts.init(document.getElementById(id));
		var option = {
		  	series : [{
		      	type:'gauge',
		      	splitNumber:5,
		      	min:lower,
            	max:upper,
            	radius : '90%',
		      	axisLine: {            	
		        	show: true,        		
			        lineStyle: {       		
			          	color: [[0.2, 'lime'],[0.8, '#1e90ff'],[1, '#ff4500']], 
			          	width: 4
			        }
                
		      	},
		      	axisTick: {  
		        	length :8, 
		        	lineStyle: {
		          		color: 'auto'
	        		}
		      	},
	     	 	splitLine: {
		        	length :10,
		        	lineStyle: {
		          		color: 'auto'
		        	}
		      	},
		      	axisLabel: {
		    	  	textStyle: {
		    	  		fontSize:10,
						
		    	  	}
		      	},
		      	pointer: {
	       	 		width:4
		      	},
		      	title : {
		        	textStyle: {
		          		fontSize: 11.5,
		          		fontWeight: 'bolder',
		          		fontStyle: 'italic',
                    color: '#fff'
		        	},
		      		offsetCenter: [0, -14]
		      	},
		      	detail : {
		        	show:true,
		        		textStyle:{
          				fontSize:13,
		          		fontWeight: 'bolder',
		          		color:'#fff'
		        	},
		        	formatter:'{value}'+'\n'+chartName
		      	},
		      	data:[{value: 0, name: unit}]
		    }]
		};
		
		this[id+'Option'] = option;
		this[id+'Chart'] = chart;
		chart.setOption(option,true);
	},
	setMachineLine : function(data,isInited){
		var machineLineChart = echarts.init(document.getElementById('machineLine'));
		var option = {
		    title : {
		        text: '车辆实时数据',
		        subtext: '车速',
		        x:'center',
		        y:'bottom'
		    },
		    tooltip : {
		        trigger: 'axis'
		    },
		    toolbox: {
		        show : false
		    },
		    grid: {
		        y: 20,
		        y2: 80,
		        x2: 40,
		        borderWidth:0
		    },
		    xAxis : [
		        {
		            type : 'category',
		            boundaryGap : true,
		            splitLine:{
		        		show:false
		            },
		            data : (function (){
		                var now = new Date();
		                var res = [];
		                var len = 15;
		                while (len--) {
		                    res.unshift(now.toLocaleTimeString().replace(/^\D*/,''));
		                    now = new Date(now - 2000);
		                }
		                return res;
		            })()
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            scale: true,
		           	splitArea:{
		            	show:true
		            },
		            name : 'km/h',
		            boundaryGap: [0.2, 0.2]
		        }
		    ],
		    series : [
		        {
		            name:'车速',
		            type:'line',
		            itemStyle: {normal: {areaStyle: {type: 'default',color:'rgba(135,206,250,0.3)'}}},
		            data:(function (){
		            	if(isInited){
			                var res = [];
			                var len = 15;
			                while (len--) {
			                    res.push(0);
			                }
			                return res;
		            	}else{
		            		return data;
		            	}
		            })()
		        }
		    ]
		};
		
		this['machineLineChart']= machineLineChart;
		machineLineChart.setTheme('macarons');
		machineLineChart.setOption(option,true);
	}
};

/*
 * 电池状态图表
 */
var carBattery = {
	init : function(){
		this.setBatteryBox('batteryBoxVoltage_0','1号单体蓄电池包电压值','V');
		this.setBatteryBox('batteryBoxTemp_0','1号动力蓄电池包温度值','℃');
	},
	//全部图表的resize
	resizeAll : function(){
		var chartIds = [];
		for(var i in this){
			if((typeof this[i]) === 'object' && i.indexOf('Chart') >= 0){
				this[i].resize();
			}
		}
	},
	setBatteryBox:function(id,name,unit){
		var chart = echarts.init(document.getElementById(id));
		var option = {
		    title : {
		        text: name,
		        x:'center'
		    },
		    tooltip : {
		        trigger: 'axis'
		    },
		    legend: {
		    	show:false,
		        data:['']
		    },
		    toolbox: {
		        show : true,
		        feature : {
		            saveAsImage : {show: true}
		        }
		    },
		    grid:{
		    	y2 : 100
		    },
		    calculable : true,
		    xAxis : [
		        {
		            type : 'category',
		            data : ['0'],
		          	axisLabel:{
		        		show:true,
		        		rotate:60
		          	}
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		          	name: unit
		        }
		    ],
		    series : [
		        {
		            type:'bar',
		            data:[0],
                  	itemStyle: {
		        		normal: {
	  						color: function(param){
			        			if(id =='batteryBoxVoltage'){
			        				var val = param.data;
							      	if(val > 3.7 || val < 3.5)
							     		return 'red';
							  		else 
							      		return 'orange';
			        			}else{
									var val = param.data;
							      	if(val > 27 || val < 25)
							     		return 'red';
							  		else 
							      		return 'orange';
			        			}
						  	}
						}
			        }
		        }
		    ]
		};
		this[id+'Option'] = option;
		this[id+'Chart'] = chart;
		chart.setOption(option,true);
	}
};	